首页 > 试题广场 >

判断子序列

[编程题]判断子序列
  • 热度指数:2837 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
给定两个字符串 S 和 T ,判断 S 是否是 T 的子序列。
即是否可以从 T 删除一些字符转换成 S。

数据范围: ,保证字符串中仅含有小写字母
示例1

输入

"nowcoder","nowcoder"

输出

true
示例2

输入

"nower","nowcoder"

输出

true
示例3

输入

"nowef","nowcoder"

输出

false
from operator import index
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
# 
# @param S string字符串 
# @param T string字符串 
# @return bool布尔型
#
class Solution:
    def isSubsequence(self , S: str, T: str) -> bool:
        # write code here
        index = 0

        for s in T:
            if S[index] == s:
                index += 1
            
            if index == len(S):
                return True

        if index == len(S):
            return True
        return False

编辑于 2024-03-28 22:22:36 回复(0)
class Solution:
    def isSubsequence(self , S: str, T: str) -> bool:
        # write code here
        i, j = 0, 0
        m, n = len(S), len(T)
        while i < m and j < n:
            if S[i] == T[j]:
                i += 1
                j += 1
            else:
                j += 1
        if i == m:
            return True
        return False
发表于 2022-08-16 14:33:38 回复(0)
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
# 
# @param S string字符串
# @param T string字符串
# @return bool布尔型
#
class Solution:
    def isSubsequence(self , S: str, T: str) -> bool:
        # write code here
        import re
        pattern = ""
        for ch in S:
            pattern += ch + "[a-z]*"
        if re.search(pattern, T) == None:
            return False
        return True

发表于 2022-03-19 01:23:12 回复(0)

问题信息

难度:
3条回答 3616浏览

热门推荐

通过挑战的用户

查看代码